package com.one.blocks.rbac.jetcache.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;


/**
 * web接口转换可以通过mapstruct，这里是为了方便缓存的序列化
 *
 * @author <a href="mailto:idler41@163.com">linfuxin</a> created by 2022-02-20 20:35:10
 */
public class LocalDateTimeFromLong extends StdDeserializer<LocalDateTime> {

    public LocalDateTimeFromLong() {
        super(LocalDateTime.class);
    }

    @Override
    public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        final long value = p.getValueAsLong();
        return value != 0 ? LocalDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneId.systemDefault()) : null;
    }
}
